//
//  TableTextFeildViewCell.m
//  customerRelation
//
//  Created by 陈文斌 on 2019/5/29.
//

#import "TableTextFeildViewCell.h"
#import "Define.h"

@interface TableTextFeildViewCell()
@property (nonatomic, strong) NSString *oldText;

@end

@implementation TableTextFeildViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.titleTextField=[[UITextField alloc]initWithFrame:CGRectMake(16, 12, 200, 20)];
        self.titleTextField.font=font15;
        self.titleTextField.textColor=FCTxtColor;
        self.titleTextField.returnKeyType = UIReturnKeyDone;
        self.titleTextField.text=@"";
        self.titleTextField.delegate = self;
        [self.contentView addSubview: self.titleTextField];
        self.btn=[UIButton buttonWithType:UIButtonTypeCustom];
        self.btn.frame=CGRectMake(self.titleTextField.frame.origin.x+self.titleTextField.frame.size.width+16, 7, 30, 30);
        self.btn.titleLabel.font=font15;
        [self.btn setTitleColor:FCBlueTxtColor forState:UIControlStateNormal];
        [self.contentView addSubview:self.btn];
        [self.btn sizeToFit];
        self.btn.hidden=YES;
        
    }
    return self;
}
-(void)setfit{
    [self.titleTextField sizeToFit];
    if(self.leftImage.image){
        self.titleTextField.x=self.leftImage.x+self.leftImage.width+8;
        self.leftImage.y=15;
    }
    if (IphonePlus) {
        self.titleTextField.y=12;
        self.btn.y=10;
    }
    [self layoutIfNeeded];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    self.oldText = textField.text;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField endEditing:YES];
    
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (range.length == 1 && string.length == 0) {
        return YES;
    }
    else if (textField.text.length >= 8) {
        textField.text = [textField.text substringToIndex:8];
        return NO;
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if ([textField.text isEqualToString:self.oldText]) return;
    if (self.textFieldShouldReturnBlock) {
        self.textFieldShouldReturnBlock(textField.text);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
